﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTFrameworkCus;
using System.Data;

public partial class Controls_dmsPaging : System.Web.UI.UserControl
{
    public IEnumerable<udv_Products> _ProductList { get; set; }
    public IEnumerable<udv_Articles> _ArticleList { get; set; }
    public IEnumerable<udv_Categories> _CategoryList { get; set; }
    public DataTable dtData { get; set; }
    public int ItemsPerPage { get; set; }
    public int NumberPagesShow { get; set; }
    public Repeater rpt { get; set; }

    private IEnumerable<udv_comment> _CommentList = null;

    public IEnumerable<udv_comment> CommentList
    {
        get { return _CommentList; }
        set { _CommentList = value; }
    }
    
    #region Private Properties
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {

            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
    #endregion

    #region PagedDataSource
    PagedDataSource _PageDataSource = new PagedDataSource();
    #endregion

    #region Private Methods
    /// <summary>
    /// Build DataTable to bind Main Items List
    /// </summary>
    /// <returns>DataTable</returns>
    private DataTable GetDataTable()
    {
        DataTable dtItems = new DataTable();

        DataColumn dcName = new DataColumn();
        dcName.ColumnName = "title";
        dcName.DataType = System.Type.GetType("System.String");
        dtItems.Columns.Add(dcName);

        DataRow row;
        for (int i = 1; i <= 100; i++)
        {
            row = dtItems.NewRow();
            row["title"] = "hmweb - Phân Trang dữ liệu DataList: &nbsp;Đây là bản ghi thứ " + i;
            dtItems.Rows.Add(row);
        }
        return dtItems;
    }

    /// <summary>
    /// Binding Main Items List
    /// </summary>
    private void BindItemsList()
    {

        //DataTable dataTable = this.GetDataTable();
        if (_ProductList != null)
        {
            if (_ProductList.Count() > ItemsPerPage)
            {
                Panel1.Visible = true;
            }
            _PageDataSource.DataSource = _ProductList.ToList();
        }
        else if (_ArticleList != null)
        {
            if (_ArticleList.Count() > ItemsPerPage)
            {
                Panel1.Visible = true;
            }
            _PageDataSource.DataSource = _ArticleList.ToList();
        }
        else if (CommentList != null)
        {
            if (CommentList.Count() > ItemsPerPage)
            {
                Panel1.Visible = true;
            }
            _PageDataSource.DataSource = CommentList.ToList();
        }
        else if (dtData != null)
        {
            if (dtData.Rows.Count > ItemsPerPage)
            {
                Panel1.Visible = true;
            }
            _PageDataSource.DataSource = dtData.DefaultView;

        }
        else if (_CategoryList != null)
        {
            if (_CategoryList.Count() > ItemsPerPage)
            {
                Panel1.Visible = true;
            }
            _PageDataSource.DataSource = _CategoryList.ToList();

        }
        else { }
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = ItemsPerPage;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;
        if (_PageDataSource.Count > 0)
        {
            this.rpt.DataSource = _PageDataSource;
            this.rpt.DataBind();
            this.doPaging();
        }

    }

    /// <summary>
    /// Binding Paging List
    /// </summary>
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - 3;
        if (CurrentPage > 3)
        {
            lastIndex = CurrentPage + 3;
        }
        else
        {
            lastIndex = 6;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 6;
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    #endregion

	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindItemsList();
        }
    }

	protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.BindItemsList();
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.BindItemsList();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.BindItemsList();
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {

        CurrentPage = 0;
        this.BindItemsList();
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }

    public string getCurrPage()
    {
         try
            {
                return (CurrentPage + 1).ToString();
            }
            catch (Exception)
            {
                return "1";
            }
               
    }
}